package com.yunhe.day322.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.day322.dao.UserDao;
import com.yunhe.day322.domain.Orders;
import com.yunhe.day322.domain.Role;
import com.yunhe.day322.domain.UserInfo;
import com.yunhe.day322.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
//    @Autowired
//    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //调用持久层方法，根据账号获取登录信息
        UserInfo userInfo = userDao.findUserByUsername(username);
        System.out.println(userInfo);

        //创建一个securty的验证用户
        User user = new User(userInfo.getUsername(),userInfo.getPassword(),userInfo.getStatus()==1,true,true,true,getRole(userInfo.getRoleList()));
        System.out.println(user);
        return user;
    }

    private Collection<? extends GrantedAuthority> getRole(List<Role> roleList) {
        ArrayList<GrantedAuthority> alist = new ArrayList<>();
        //遍历用户的角色进行
        for (Role role : roleList){
            SimpleGrantedAuthority sga = new SimpleGrantedAuthority(role.getRoleName());
            alist.add(sga);

        }

        return alist;
    }
    /**
     * 新增用户
     */
    @Override
    public void save(UserInfo userInfo) {
//       userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));

        userDao.save(userInfo);
    }
    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<UserInfo> findAllUsers(Integer page,Integer size) {
        PageHelper.startPage(page,size);
        return userDao.findAllUsers();
    }


    @Override
    public UserInfo findUserInfoById(Integer id) {

        return userDao.findUserInfoById(id);
    }

    @Override
    public List<Role> findOtherRole(Integer id) {
        List<Role> otherRole = userDao.findOtherRole(id);
        return otherRole;
    }

    @Override
    public UserInfo findUserById(Integer id) {

        return userDao.findUserById(id);
    }

    @Override
    public void addRoleToUser(Integer userId, Integer[] ids) {
        for(Integer roleid : ids){
            this.userDao.addRoleToUser(userId,roleid);
        }
    }
    @Override
    public void deleteUserRole(int id) {
        this.userDao.deleteUserRole(id);
    }
    @Override
    public void deleteById(int id) {

        userDao.deleteById(id);

    }
}
